# -*- coding: utf-8 -*-
import scrapy
from com.wltjack.scrapy.douban.douban.items import DoubanItem

class DoubanSpiderSpider(scrapy.Spider):
    # 爬虫的名称
    name = 'douban_spider'
    # 允许抓取的域名
    allowed_domains = ['movie.douban.com']
    # 开始抓取的地址
    start_urls = ['https://movie.douban.com/top250']

    def parse(self, response):
        # 打印返回结果
        # print(response.text)
        movie_list = response.xpath(".//div[@class='article']//ol[@class='grid_view']/li")
        for movie_item in movie_list:
            doubanItem = DoubanItem()
            doubanItem['serial_number'] = movie_item.xpath(".//div[@class='item']//em/text()").extract_first()

            movie_names = movie_item.xpath(".//div[@class='info']//div[@class='hd']/a//span/text()").extract()
            temp_movie_name = ''
            for name_item in movie_names:
                temp_movie_name += "".join(name_item.split())
            doubanItem['movie_name'] = temp_movie_name
            descs = movie_item.xpath(".//div[@class='info']//div[@class='bd']//p/text()").extract()
            temp_desc = ''
            for desc_item in descs:
                temp_desc += desc_item.strip()
                # temp_desc += "".join(desc_item.split())
            doubanItem['introduce'] = temp_desc

            doubanItem['star'] = movie_item.xpath(".//div[@class='info']//div[@class='bd']//div[@class='star']/span[@class='rating_num']/text()").extract_first()
            doubanItem['evaluate'] = movie_item.xpath(".//div[@class='info']//div[@class='bd']//div[@class='star']/span[last()]/text()").extract_first()
            doubanItem['describe'] = movie_item.xpath(".//div[@class='info']//div[@class='bd']//p[@class='quote']/span[@class='inq']/text()").extract_first()

            yield doubanItem

        # 解析下一页
        next_page_link = response.xpath("//span[@class='next']/link/@href").extract_first()
        if next_page_link:
            # callback=self.parse : 请求回调
            yield scrapy.Request("https://movie.douban.com/top250" + next_page_link, callback=self.parse)

